package com.lazy.book.service.system.impl;

import com.lazy.book.common.entity.PageResult;
import com.lazy.book.common.entity.SocketMessage;
import com.lazy.book.common.entity.TransferMessage;
import com.lazy.book.common.enums.MessageTransferScopeEnum;
import com.lazy.book.common.enums.SocketChannelEnum;
import com.lazy.book.common.utils.BeanCopyUtils;
import com.lazy.book.common.utils.JsonUtils;
import com.lazy.book.common.utils.PageUtils;
import com.lazy.book.common.utils.Utils;
import com.lazy.book.mapper.system.SysMessageMapper;
import com.lazy.book.pojo.system.pojo.dto.sysmessage.Message;
import com.lazy.book.pojo.system.pojo.dto.sysmessage.PayloadBody;
import com.lazy.book.pojo.system.pojo.dto.sysmessage.SysMessageListDTO;
import com.lazy.book.pojo.system.pojo.po.SysMessage;
import com.lazy.book.pojo.system.pojo.vo.sysmessage.SysMessageVO;
import com.lazy.book.service.common.WebsocketRedisService;
import com.lazy.book.service.system.SysMessageService;
import com.lazy.book.service.system.SysMessageUserService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 消息管理 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-04-21
 */
@Service
@RequiredArgsConstructor
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {

    private final SysMessageUserService sysMessageUserService;

    private final WebsocketRedisService websocketRedisService;

    @Override
    public void create(Message dto) {
        SysMessage sysMessage = BeanCopyUtils.copy(dto, SysMessage.class);
        save(sysMessage);
        Long messageId = sysMessage.getId();
        List<Object> receiverIds = dto.getReceiverIds();
        sysMessageUserService.batchInsert(messageId, receiverIds);

        PayloadBody body = new PayloadBody();
        body.setTitle(dto.getTitle());
        body.setContent(dto.getContent());
        SocketMessage message = SocketMessage.builder().data(JsonUtils.toJsonString(body)).channel(SocketChannelEnum.MESSAGE)
                .scope(MessageTransferScopeEnum.SOCKET_CLIENT).build();
        TransferMessage msg = TransferMessage.builder().message(message).fromUser(dto.getSenderId().toString()).toPushAll(false).toUsers(dto.getReceiverIds())
                .build();
        websocketRedisService.sendServiceToWs(msg);
    }

    @Override
    public PageResult<SysMessageVO> page(SysMessageListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_MESSAGE);
        if (Utils.isNotBlank(dto.getTitle())) {
            wrapper.and(SYS_MESSAGE.TITLE.like(dto.getTitle()));
        }
        Page<SysMessage> page = page(dto.buildPage(), wrapper);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<SysMessageVO> list(SysMessageListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(SYS_MESSAGE);
        if (Utils.isNotBlank(dto.getTitle())) {
            wrapper.and(SYS_MESSAGE.TITLE.like(dto.getTitle()));
        }
        return list(wrapper).stream().map(sysMessage -> BeanCopyUtils.copy(sysMessage, SysMessageVO.class)).collect(Collectors.toList());
    }

    @Override
    public SysMessageVO detail(Object id) {
        SysMessage sysMessage = getById(id);
        return BeanCopyUtils.copy(sysMessage, SysMessageVO.class);
    }



}
